Recurrent neural network based high-precision position compensation control of magnetic levitation system

For improving the dynamic quality and steady-state performance, the hybrid controller based on recurrent neural network (RNN) is designed to implement the position control of the magnetic levitation ball system in this study. This hybrid controller consists of a baseline controller, an RNN identifier, and an RNN controller. In the hybrid controller, the baseline controller based on the control law of proportional-integral-derivative is firstly employed to provide the online learning sample and maintain the system stability at the early control phase. Then, the RNN identifier is trained online to learn the accurate inverse model of the controlled object. Next, the RNN controller shared the same structures and parameters with the RNN identifier is applied to add the precise compensation control quantity in real-time. Finally, the effectiveness and advancement of the proposed hybrid control strategy are comprehensively validated by the simulation and experimental tests of tracking step, square, sinusoidal, and trapezoidal signals. The results indicate that the RNN-based hybrid controller can obtain higher precision and faster adjustment than the comparison controllers and has strong anti-interference ability and robustness.


Scientific Reports
| (2022) 12:11435 | https://doi.org/10.1038/s41598-022-15638-0 www.nature.com/scientificreports/ position of the maglev ball, which obtained a better track performance of the step signal than conventional PID controller 27 . Sun et al. presented a supervisor control method based on the RBF neural network and achieved an excellent tracking performance in the flexible and time-delay control of the maglev train system 28 .
Through the above literature review, it can be found that these neural networks used in the control systems belong to the feedforward neural networks, which can improve the control performance of the magnetic levitation system by approximating a nonlinear function without prior knowledge of the closed-loop control system 26 . However, the parameter updating of these feedforward neural networks reckon without the historical information of the control system, which limits the nonlinear approximation of the controlled object to some extent, thereby hindering the further improvement in the control performance of the magnetic levitation system.
The recurrent neural network (RNN) can acquire the dynamic response of the control system by using delays of an internal feedback loop, which has the ability to deal with the time-varying control system, especially in nonlinear and uncertain scenarios 29,30 . Lin et al. designed a hybrid computed force control method with an RNN uncertainty observer, which improved the tracking accuracy in position control of the magnetic levitation system 30 . Fatemimoghadam et al. proposed an adaptive backstepping control scheme based on a projection RNN for the magnetic levitation system, which achieved a better control performance than the sliding mode control method 31 . Jafari and Hagan applied RNN to the model reference control of the magnetic levitation system and obtained better performance than linear PID controllers 32 . Hou et al. embedded a recurrent feature selection neural network to an intelligent global sliding mode controller (GSMC) and acquired superior performance than traditional GSMC 33 .
Therefore, motived by the mentioned superiorities of the RNN in the controller design of the nonlinear time-varying system, a hybrid controller based on RNN is proposed to improve the control performance in the position control of the magnetic levitation ball system in this article. The main contributions are summarized in the following.
1. An intelligent controller based on the accurate compensation control is developed to ensure both dynamical performance and steady-state performance of the control system, which consists of a PID-based baseline controller, an RNN identifier, and an RNN controller. 2. The RNN identifier is designed to online learn the inverse model of the control system, and the learning parameters are passed to the RNN controller sharing the same structure as the RNN identifier in real-time, which achieves the accurate compensation control. 3. The simulation analysis and experimental verification are shown comprehensively to prove the effectiveness and advancement of the proposed RNN-based intelligent controller and the improvement of the transient performance and the steady-state accuracy with a certain robustness.
The remainder of this article is organized as follows. Section "Problem formulation" formalizes the problem to be solved. Then, the hybrid controller is specifically designed in Section "Controller design". Next, simulation analysis and experimental verification are presented in Sections "Simulation analysis" and "Experimental verification" respectively. Finally, Sections "Discussion" and "Conclusion" give the conclusion of this study and discuss the potential future work.

Problem formulation
This study focuses on the position control of a magnetic levitation ball system, and the schematic diagram of its physical entity is shown in Fig. 1. The system is mainly composed of an electromagnetic coil, a steel ball, a power amplifier, a photoelectric position sensor, a data acquisition and control card, and a computer. In this system, the input voltage of the power amplifier is controlled to make the coil current generate the appropriate electromagnetic force, thereby the position of the steel ball can be controlled.
To describe the position control problem of the magnetic levitation ball system, as shown in Fig. 1, let F(i, x) be the electromagnetic force at the current i of the electromagnetic coil and the position x of the steel ball, U in represents the input voltage of the power amplifier, m and g denote the mass of the steel ball and the gravitational acceleration respectively. In addition, this study supposes that there is no magnetic flux leakage and iron coil www.nature.com/scientificreports/ magnetoresistance, the electromagnetic force of the steel ball is concentrated at the center of its gravity, and the output current of the power amplifier has a linear relationship with its input voltage. Concretely, the mathematical model of the control object can be established in the following. First, the steel ball is only subject to the electromagnetic force F(i, x) and its gravity mg in the controlled system, ignoring other interference forces on the ball. Thus the dynamic equation of the steel ball in the vertical direction can be described as follows where ẍ d 2 x(t)/dt 2 denotes the acceleration of the ball at position x.
Then, based on Kirchoff 's law and Biot-Savart's law, the electromagnetic force F(i, x) can be calculated as follows 22 where K = µ 0 AN 2 /4 , and parameters µ 0 , A , and N represent the vacuum permeability, permeability area, and coil turn respectively.
When the ball is in the equilibrium position, its acceleration is zero and can be expressed as where F(i 0 , x 0 ) denotes the electromagnetic force at the equilibrium point (i 0 , x 0 ). Next, according to Faraday's law and Kirchoff 's law, the relationship between control voltage and current in the electromagnet winding can be expressed as follows where R and L are the resistance and self-inductance of the electromagnetic coil respectively.
Finally, the magnetic levitation ball system can be described jointly by the above equations, and the expression is as follows Expanding the Taylor series at the equilibrium point (i 0 , x 0 ) , the electromagnetic force F(i, x) is equivalent to is the remaining high-order term of F(i, x).
When omitting the high-order term, the kinetic equation of the steel ball can be rewritten as For the magnetic levitation ball system, the input voltage U in of the power amplifier is the input of the control system, while the output voltage U out of the sensor module is the output of the control system. Taking the Laplace transformation of Eq.  www.nature.com/scientificreports/ So the controllability matrix P and the observability matrix Q of the magnetic levitation ball system can be calculated by From Eq. (12), the rank of matrices P and Q can be obtained as follows As known from Eq. (13), the rank of the controllability matrix P is equal to the dimension of the state variables, while the rank of the observability matrix Q is equal to the dimension of the output vector. In other words, the magnetic levitation ball system is both controllable and observable. Consequently, the controller can be designed to make the system stable.

Controller design
In this section, a hybrid controller is designed for position control of the magnetic levitation ball system. As shown in Fig. 2, the designed controller mainly consists of a PID controller, an RNN controller, and an RNN identifier.
For the hybrid controller, the PID controller is the baseline controller, and the RNN controller is the compensation controller. Where the PID controller is mainly applied to provide online samples for the RNN identifier to learn the inverse model of the controlled object in real-time and also used to maintain the stability of the control system when the RNN is untrained at the early control phase. And the RNN controller sharing the same network structure as the RNN identifier is utilized to carry out accurate compensation control.
In addition, the RNN identifier is employed to learn the inverse model of the controlled object online, considering the historical data of the control system. And the learning parameters from the RNN identifier are passed to the RNN controller in real-time. Therefore, the proposed controller can achieve the high-precision position control of the magnetic levitation ball, benefiting from that the RNN identifier accurately learns the inverse model of the control object, and the RNN controller implements exact compensation control in the real-time.
Concretely, the control law u(k) of the hybrid controller is designed as follows where u b (k) and u c (k) denote the control law of PID and RNN controllers respectively.
Baseline controller. With the characteristics of clear functionality, structure simplicity, ease of use, and applicability, the PID controller has been regarded as the simplest and yet most efficient scheme for lots of industrial control problems 34 . Consequently, this article takes the PID controller as a baseline controller in the proposed hybrid control strategy.
In practice, as a baseline controller of the hybrid control strategy, the PID controller is employed to provide the online learning samples and maintain the system stability at the early control phase, especially when the neural network is untrained. Concretely, the control law u b at the moment k is defined as follows www.nature.com/scientificreports/ where k p is proportion gain, k i is integration gain, and k d is differentiation gain.

RNN controller.
As one main part of the hybrid control strategy, the RNN controller is utilized for the position compensation in the magnetic levitation ball system. Concretely, as shown in Fig. 3a, the RNN controller consists of an input layer, a hidden layer, and an output layer. For the RNN controller, the tracking error e(k) is taken as input, then the input layer at moment k can be defined as follows The hidden layer of the RNN controller at moment k is calculated as follows where w i denotes the ith weight between the input and hidden layers, ϕ i represents the ith weight between the moments k − 1 and k in the hidden layer, b i is the ith bias in the hidden layer, σ (·) is the tanh activation function.
The output layer of the RNN controller at moment k is obtained as follows where v i denotes the ith weight between the hidden and output layers. Substitute Eqs. (16) and (17) into Eq. (18), then the control law u c of the RNN controller at the moment k can be obtained as follows It is worthy to note that the RNN controller only carries out the forward propagation calculation but does not carry out the back propagation calculation, and the solving parameters ( w i , ϕ i , b i , and v i ) are real-time passed from the RNN identifier. Finally, the compensation control law can be obtained according to Eq. (19).

RNN identifier.
The RNN identifier shares the same network structure as the RNN controller. To learn the inverse model of the controlled object more accurately, the RNN identifier performs both the forward and back propagation calculations.
Similarly to the RNN controller, as shown in Fig. 3b, the forward propagation of the RNN identifier at the moment k can be calculated by In the back propagation process, define L(k) the loss function at moment k as follows where u(k) denotes the actual control law of the hybrid controller at moment k, û(k) represents the estimated control law of the RNN identifier at moment k.
The gradient of the weight v i is calculated by Let δ i (k) denote the partial derivative of L(k) with respect to z i (k) , then the formula for δ i (k) is as follows The gradient of the weight φ i is calculated by The gradient of the weight b i is calculated by The gradient of the weight w i is calculated by To simplify the parameter updating process, uniformly mark v i , ϕ i , b i , and w i as θ , then the parameters of the RNN identifier can be updated as follow where hyperparameters η and γ are the learning rate and the penalty coefficient respectively, and the smoothing term ε = 1e − 8 is used to avoid division by zero.

Simulation analysis
Simulation setup. To demonstrate the effectiveness of the designed controller, the simulation tests are carried out on the Matlab/Simulink. For the magnetic levitation ball system investigated in this article, the physical parameters are described in Table 1.
Substitute the parameters described in Table 1 into Eq. (5), the numerical transfer function of the controlled object can be described as follows (20) (28), the baseline controller based on PID (PID), the hybrid controller based on PID and BPNN (PID + BPNN), and the hybrid controller based on PID and RNN (PID + RNN) are quantitatively compared on the following measurements, namely, the mean absolute error (MAE), the rootmean-square error (RMSE), the integral time absolute error (ITAE), and the integral time square error (ITSE). The formulations of these four measurements are expressed as follows The tracking controls of the step, square, sinusoidal, and trapezoidal signals are implemented in the simulation tests. In particular, for the tracking controls of step and square signals, the overshoot and settling time are considered besides MAE and RMSE. For the tracking controls sinusoidal and trapezoidal signals, the steady state error is considered besides MAE and RMSE. Moreover, for the fair comparison of the different control strategies, we try our best to make control performance to be the best by adjusting the appropriate parameters of each controller. Take the tracking controls of sinusoidal and trapezoidal signals as examples, the simulation results of the RNN-based hybrid controller under different hyper-parameters are shown in Fig. 4.
Ultimately, for the simulation tests, the optimal parameters of the different controllers are described in Table 2.
Tracking performance. For the simulation tests, the control responses of tracking step and square signals under the different comparison controllers are presented in Figs. 5 and 6, where the subgraph on the left is the enlarged version in the red circle of the corresponding subgraph on the right. The performance evaluations of these tracking controls are described in Table 3. As shown in Figs. 5 and 6, three controllers all can realize the tracking control of the magnetic levitation ball, but the separate PID controller has a larger overshoot and a longer settling time. On the contrary, the hybrid controllers can lower the overshoot and shorten the settling time of the control responses to some extent. Moreover, the hybrid controller based on PID and RNN can obtain the best dynamic quality by further lowing the overshoot and shortening the settling time of the control responses, compared with the hybrid controller based on PID and BPNN.   www.nature.com/scientificreports/ Concretely, as shown in Table 5, for step signal tracking control, the RNN-based hybrid controller reduces overshoot by 34.53% and the settling time by 29.18%, compared with the BPNN-based hybrid controller. For square signal tracking control, the RNN-based hybrid controller reduces the overshoot and settling time by 35.85% and 41.96% respectively, compared with the BPNN-based hybrid controller. Therefore, the RNN-based hybrid controller can obtain excellent dynamic performances in the simulation of tracking step and square signals.    Figs. 7 and 8, and the performance evaluations of the tracking controls are described in Table 4.
It can be known from Figs. 7 and 8 that, the hybrid controllers can improve the control response and lower the tracking error, compared with the separate baseline controller based on PID. In addition, the hybrid controller based on PID and RNN obtains the best control quality and the lowest tracking error in all comparison controllers, benefiting from the RNN identifier builds a more accurate inverse model of the controlled object.    Table 4, it can be found that when tracking sinusoidal signal, the hybrid controller based on PID and RNN lowers the control error above 80%, compared with the hybrid controller based on PID and BPNN. When tracking trapezoidal signal, the hybrid controller based on PID and RNN lowers the steady-state error, MAE, and RMSE above 40%, 80%, and 80% respectively, compared with the hybrid controller based on PID and BPNN. Consequently, whether tracking sinusoidal signal or trapezoidal signal, the RNN-based hybrid controller can significantly improve the control performances compared with the BPNN-based hybrid controller.

Experimental verification
Experimental setup. To further verify the effectiveness and the advancement of the RNN-based hybrid controller, the hardware-in-loop platform shown in Fig. 9 is used to conduct experimental research on the position control of the magnetic levitation ball system. The tracking controls of the step, square, sinusoidal, and trapezoidal signals are carried out in the Matlab/Simulink real-time workshop.
In the comparison controllers, hyper-parameters play a critical role in controller performance. Take the tracking controls of sinusoidal and trapezoidal signals as examples, the experiment results of the RNN-based hybrid controller under different hyper-parameters are displayed in Fig. 10. Therefore, for the fair comparison of the different control schemes in the experimental tests, we also try our best to make control performance to be the best by adjusting the appropriate parameters of each controller. Specifically, for the experiment tests, the optimal parameters of the different controllers are described in Table 5.   Table 5. The optimal parameters of the different controllers in the experiment tests.

All signals
Step Square Trapezoidal Sinusoidal It can be learned from Figs. 9 and 10 that the hybrid controller can achieve lower overshoot and shorten settling time compared with the baseline controller when tracking continuous step and square signals. And the hybrid controller based on PID and RNN can acquire the best control quality among all the comparative controllers. Concretely, the performance evaluations of these tracking controls are quantitatively described in Table 6.   Step (Fig. 11 www.nature.com/scientificreports/ As shown in Table 6, for both continuous step and square signals, the RNN-based hybrid controller has an obvious reduction in terms of overshoot and settling time compared with the BPNN-based hybrid controller. Especially, in terms of the steady-state error, the RNN-based hybrid controller has an improvement of 37.12% and 35.53% for continuous step signal, and 12.86% and 55.68% for square signal, compared with the BPNN-based hybrid controller. As a result, through the comparative analysis of the experimental results, it can be easily found that the RNN-based hybrid controller can improve both dynamic and steady state performances.
In addition, to preferably demonstrate the control performances, the tracking controls of sinusoidal and trapezoidal signals are tested in the experiment platform, and the control responses are drawn in Figs. 13 and 14.
It can be seen that the hybrid controllers can track the reference signals better and have lower steady-state error compared with the baseline controller. Particularly, the position tracking accuracy of the magnetic levitation ball is higher than the BPNN-based hybrid controller when applying the RNN-based hybrid controller. Concretely, the performance evaluations of the tracking controls are quantitatively described in Table 7.
From Table 7, it can be found that for tracking sinusoidal and trapezoidal signals, the RNN-based hybrid controller lowers the steady state error by about 60% and 20% respectively, compared with the BPNN-based hybrid controller. Meanwhile, the RNN-based hybrid controller reduces the MAE and RMSE by above 70% and 30% respectively, compared with the BPNN-based hybrid controller. Consequently, the RNN-based hybrid controller has not only the best dynamic tracking performance but also the lowest steady state error among all the comparison controllers.
Robust performance. In this section, the tracking control experiments of both sawtooth and stochastic signals with constant disturbance are first utilized to indicate the disturbance rejection ability of the RNN-based intelligent controller. The experiment results are drawn in Fig. 15, where the four subgraphs above show the  www.nature.com/scientificreports/ anti-interference performance under tracking sawtooth signal, and the four subgraphs below display the antiinterference performance under tracking stochastic signal. It can be found from Fig. 15 that whether tracking a sawtooth or stochastic signal, the magnetic levitation ball can respond to the disturbing reference quickly. Thus the RNN-based intelligent controller can suppress disturbance effectively because of the quick response and accurate compensation from the RNN-based inverse model controller in real-time.
In addition, the tracking control experiments of both sinusoidal and trapezoidal signals with stochastic disturbance are further employed to demonstrate the disturbance rejection ability of the RNN-based hybrid controller. The experiment results are drawn in Fig. 16, where the four subgraphs above denote the anti-interference performance under tracking sinusoidal signal, and the four subgraphs below represent the anti-interference performance under tracking sinusoidal signal.
It can be seen from Fig. 16 that whether tracking sinusoidal or trapezoidal signal, the disturbed magnetic levitation ball can track the desired reference quickly, benefiting from the RNN-based hybrid controller that can suppress disturbance effectively. Consequently, the closed-loop control system has strong robustness to some extent when using the RNN-based hybrid controller.

Discussion
The magnetic levitation ball system is a typical nonlinear and unstable system. For improving its control accuracy and tracking performance, this study presents an intelligent controller based on a PID-based baseline controller, an RNN identifier, and an RNN controller. The RNN identifier is designed to learn the inverse dynamics model of the controlled object online then the learning parameters are passed to the RNN controller in real-time, carrying out an accurate compensation control. And the comprehensive simulation and experiment results show that the proposed controller can improve the transient performance and lower the steady-state error with excellent robustness compared with the traditional PID controller and BPNN-based internal model controller.
To further illustrate the effectiveness and the advancement of the designed controller, some advanced control approaches of the magnetic levitation ball system are experimentally compared in this section. It is worth noticing that all comparisons are performed on the same or similar experimental platform. In addition, the different methods are not compared under the time integral evaluation metric, considering the different duration of the tracking experiment. www.nature.com/scientificreports/ First, as one of the effectively intelligent control approaches 35 , fuzzy control is considered to compare with the proposed controller. The experiment results of the tracking step signal show that the fluctuation range of tracking error is [− 0.05, 0.05] using the Takagi-Sugeno fuzzy controller (TSFC) 36 . The error range of the TSFC is obviously larger than that of the proposed controller, which can obtain the error range of [− 0.0166, 0.0176] when tracking the step signal.
Then, an advanced sliding mode control method named adaptive nonsingular terminal sliding mode control (ANTSMC) based on reduced order generalized proportional integral observer (RGPIO) 7 is employed to compare with the proposed controller. As shown in Table 8, the RMSEs of the ANTSMC + RGPIO are 0.2974 and 0.1367 respectively when tracking square and sinusoidal signals, which are larger than those of the proposed PID + RNN controller.
Finally, a nonlinear autoregressive exogenous-type neural network controller (NARX-NNC) 22 is utilized to compare with the proposed controller. For the sinusoidal signal tracking, the RMSE using NNC is 0.0472, which is obviously larger than the 0.0145 obtained by the proposed controller. Besides, the fuzzy inference-based neural network compensation controller (FI-NNCC) 26 is applied to compare with the proposed controller. As shown in Table 8, the steady state error of the FI-NNCC is a little bit larger compared with the proposed controller overall.
As a result, it can be seen from the above comparative analysis that the proposed intelligent controller based on RNN has better control performance and tracking accuracy than other advanced control approaches to the magnetic levitation ball system.

Conclusion
In this study, the hybrid control based on RNN is proposed to achieve the high-precision position control of the magnetic levitation ball system. The main contribution of this study is that the control quantity can be adaptively compensated with high precision by the hybrid controller without establishing an accurate mathematical model of the control system. Moreover, the innovation is that the RNN controller implements high-precision control while the RNN identifier accurately learns the inverse model of the controlled object online, considering the historical data of the control system. Ultimately, the simulation and experiment results demonstrate that the RNN-based hybrid controller can improve the transient performance and lower the steady-state error with certain robustness compared with the BPNN-based hybrid controller.  www.nature.com/scientificreports/ The proposed RNN-based hybrid scheme has been successfully applied to the magnetic levitation ball system with a single-degree-of-freedom. And the research in this article may provide some helpful references for the application of the proposed approach in the real-time control process. In addition, the proposed hybrid controller can facilitate deployment to the second-order control system with nonlinear, time-varying, and uncertain characteristics. In future work, on the one hand, we will try to design the hybrid controller based on RNN to the multi-degree of the freedom control system; on the other hand, we will try to study the high order control problem by using an RNN-based hybrid control strategy.